<?php

class m_group_barang extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    function get_count_all_group_barang() {
        $sql = "SELECT COUNT(group_barang_cd)'total' FROM group_barang";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    //cek data
    function get_count_all_group_barang_by_kode($params) {
        $sql = "SELECT COUNT(*) 'total' FROM group_barang WHERE group_barang_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    //cek data
    function get_count_all_group_barang_by_kode_nama($params) {
        $sql = "SELECT COUNT(*) 'total' FROM group_barang WHERE group_barang_cd = ? AND group_barang_name = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    //cek data
    function get_count_group_barang_by_kategorigroup_barang_cd($params) {
        $sql = "SELECT COUNT(*) 'total' FROM group_barang WHERE kategorigroup_barang_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    // get user profil
    function get_all_group_barang() {
        $sql = "SELECT a.* FROM group_barang a
                ORDER BY a.gpbarang_name ASC";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_group_barang_by_id($params) {
        $sql = "SELECT a.* FROM group_barang a
                WHERE a.gpbarang_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    // insert
    function insert($user_id) {
        // update by this date
        $sql = "INSERT INTO group_barang VALUES (?, ?, ?, ?, NOW())";
        return $this->db->query($sql, $user_id);
    }

    // edit
    function edit($user_id) {
        // update by this date
        $sql = "UPDATE group_barang SET group_barang_name = ?,kategorigroup_barang_cd = ?, mdb = ? WHERE group_barang_cd = ?";
        return $this->db->query($sql, $user_id);
    }

    // delete
    function delete($user_id) {
        // update by this date
        $sql = "DELETE FROM group_barang WHERE group_barang_cd = ?";
        return $this->db->query($sql, $user_id);
    }

    /*
     * LAPORAN
     */

    // get user profil
    function get_all_group_barang_laporan() {
        $sql = "SELECT a.*, COUNT(b.barang_id) AS 'total_barang', SUM(b.barang_harga_beli) AS 'total_harga_beli', SUM(b.barang_nilai_buku) AS 'total_nilai_buku' FROM group_barang a
                LEFT JOIN barang b ON a.gpbarang_cd = b.gpbarang_cd
                GROUP BY b.gpbarang_cd";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_all_group_barang_by_gpbarang_cd_laporan($param) {
        $sql = "SELECT a.*, COUNT(b.barang_id) AS 'total_barang', SUM(b.barang_harga_beli) AS 'total_harga_beli', SUM(b.barang_nilai_buku) AS 'total_nilai_buku' FROM group_barang a, barang b
                WHERE a.gpbarang_cd = b.gpbarang_cd AND a.gpbarang_cd = ?
                GROUP BY b.gpbarang_cd";
        $query = $this->db->query($sql, $param);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_all_group_barang_pagination($params) {
        $sql = "SELECT a.*, b.kategorigroup_barang_name FROM group_barang a
                JOIN kategorigroup_barang b ON a.kategorigroup_barang_cd =b.kategorigroup_barang_cd
                ORDER BY a.group_barang_cd ASC
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

}
